//
//  THFileManager.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THFileManager.h"
#import <sys/xattr.h>

@implementation THFileManager

static THFileManager *shareFileObj = nil;

+ (instancetype)shareManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareFileObj = [[THFileManager alloc]init];
    });
    
    return shareFileObj;
}

+ (NSString*)getDocumentPath
{
    NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [arrPath objectAtIndex:0];
    return documentsDirectory;
}

- (BOOL)hqFileExistsAtPath:(NSString*)filePath
{
    return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
}

- (BOOL)hqFileExistsAtPath:(NSString*)filePath isDirectory:(BOOL*)isDirectory
{
    return [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:isDirectory];
}

- (BOOL)hqCreateDirectory:(NSString*)dir attribute:(NSDictionary*)attr
{
    NSFileManager *fileMangaer = [NSFileManager defaultManager];
    
    BOOL result = [fileMangaer createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:attr error:nil];
    
    return result;
}

- (BOOL)hqCreateFileAtPath:(NSString*)path content:(NSData*)contentData
{
    NSFileManager *fileMangaer = [NSFileManager defaultManager];
    BOOL flag       = NO;
    BOOL isExisted  = [fileMangaer fileExistsAtPath:path isDirectory:&flag];
    
    BOOL result     = YES;
    if(!isExisted)
    {
        result = [fileMangaer createFileAtPath:path contents:contentData attributes:nil];
    }
    return result;
}

/*删除文件*/
- (void)hqDeleteFileAtPath:(NSString*)path
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager removeItemAtPath:path error:nil];
}

/*获取path下面文件夹路径*/
- (NSArray*)contentsOfDirectoryAtPath:(NSString*)pathDir
{
    if(pathDir.length > 0)
    {
        NSError *err = nil;
        return [[NSFileManager defaultManager] contentsOfDirectoryAtPath:pathDir error:&err];
    }
    else
    {
        return nil;
    }
}

+ (BOOL) hqSetSkipBackupAttribute:(NSString *)path
{
    NSString * strVersion = [[UIDevice currentDevice] systemVersion];
    float fVersion = 0.0;
    
    if (strVersion.length > 0) {
        fVersion = [strVersion floatValue];
    }
    
    BOOL result = NO;
    
    NSURL * url = [NSURL fileURLWithPath:path];
    
    if (fVersion >= 5.1f) {
        result = [self hqAddSkipBackupAttributeToItemAtURL_iOS5_1:url];
    }
    
    if ((fVersion > 5.0f) && (fVersion < 5.1f)) {
        result = [self hqAddSkipBackupAttributeToItemAtURL:url];
    }
    
    return result;
}

+ (BOOL)hqAddSkipBackupAttributeToItemAtURL_iOS5_1:(NSURL *)URL
{
    BOOL success = NO;
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:[URL path]]) {
        NSError *error = nil;
        success = [URL setResourceValue:[NSNumber numberWithBool:YES]
                                 forKey:NSURLIsExcludedFromBackupKey error:&error];
        
        if (!success) {
            NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
        }
    }
    
    return success;
}

+ (BOOL)hqAddSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    int result = NO;
    
    if ([[NSFileManager defaultManager] fileExistsAtPath:[URL path]]) {
        const char *filePath = [[URL path] fileSystemRepresentation];
        
        const char  *attrName = "com.apple.MobileBackup";
        u_int8_t    attrValue = 1;
        
        result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    }
    
    return result;
}

#pragma mark -- plist
- (NSDictionary*)hqLoadDictInfoPlistName:(NSString*)plistName
{
    NSString *filtPath = [[NSBundle mainBundle] pathForResource:plistName ofType:([plistName rangeOfString:@"plist"].location == NSNotFound) ? @"plist" : nil];
    
    return [self hqLoadDictInfoPlistPath:filtPath];
}

- (NSDictionary*)hqLoadDictInfoPlistPath:(NSString *)filePath
{
    return [NSDictionary dictionaryWithContentsOfFile:filePath];
}


+ (float) fileSizeWithPath:(NSString *)path
{
    NSDictionary * fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
    
    unsigned long long length = [fileAttributes fileSize];
    
    return length/1024.0;
}

@end
